﻿# -*- coding:utf-8 -*-
# 
from django.shortcuts import render_to_response
from django.template import RequestContext, Context
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from models import Task, History, Severity, Statusresolution, Status, Kind 
from forms import TaskForm

def index(request):
    return render_to_response('index.html', locals())
    
@login_required    
def task_create(request):   
    #project = get_object_or_404(Project, norm_name = prjname)
    # check user in project
    #users = User.objects.filter(project__id=project.id)
    #if not request.user in users:
    #    return error_403()
    #    
    if request.method == 'POST':
        form = TaskForm(request.user, request.POST)
        if form.is_valid():
            taskinst = form.save(commit=False)
            #taskinst.serial = Task.objects.filter(project = project.id).count() + 1   
            #taskinst.severityorder = SEVERITY_ORDER.index(taskinst.severity)
            taskinst.save()
            #h = History(task=taskinst, action='Task Created', user=request.user)
            #h.save();
            #return HttpResponseRedirect(reverse('homesite.bugs.views.taskinfo', args=[project.norm_name, taskinst.id]))
            return HttpResponseRedirect(reverse('ktit.task.views.index', args=[]))
    else:
        form = TaskForm(request.user)
    context = Context({
      'form': form,
    })        
    return render_to_response('form_task_create.html', context, context_instance=RequestContext(request))
 